package com.example.sonny.util.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sonny.bean.ChatBean;
import com.example.sonny.dutest.R;

import java.util.ArrayList;

/**
 * Created by Sonny on 2015/11/15.
 */
public class ChatListAdapter extends BaseAdapter {

    ArrayList<ChatBean> chats;
    LayoutInflater inflater;
    private Context context;

    public ChatListAdapter(Context context,ArrayList<ChatBean> chats) {
        this.chats = chats;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return chats.size();
    }

    @Override
    public Object getItem(int position) {
        return chats.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChatBean chat = chats.get(position);

        ViewHolder holder;
        if(convertView == null){

            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.chat_list_row,null);
            holder.avatar_iv = (ImageView)convertView.findViewById(R.id.avartar_iv);
            holder.target_tv = (TextView)convertView.findViewById(R.id.target_tv);
            holder.message_tv = (TextView)convertView.findViewById(R.id.message_tv);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }


        int id = context.getResources().getIdentifier(chat.getLatest().getContent(),"drawable",context.getPackageName());
        holder.avatar_iv.setImageResource(id);
        holder.target_tv.setText(chat.getTarget());
        holder.message_tv.setText(chat.getLatest().getContent());

        return convertView;
    }

    class ViewHolder{
        TextView target_tv;
        TextView message_tv;
        ImageView avatar_iv;
    }
}
